package android.slc.extras.system.domain

import android.app.AlertDialog
import android.content.DialogInterface
import android.slc.appbase.R
import android.slc.appbase.ui.vd.BaseAppVd
import android.slc.popup.SlcPopup2
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.IntentUtils
import com.karumi.dexter.Dexter
import com.karumi.dexter.MultiplePermissionsReport
import com.karumi.dexter.listener.multi.BaseMultiplePermissionsListener
import com.karumi.dexter.listener.multi.CompositeMultiplePermissionsListener

class StartupPermissionsVd: BaseAppVd<StartupPermissionsVmBox>() {

    override fun initMustCondition() {
        super.initMustCondition()
        vmBox.requestPermissions.observe(lifecycleOwner) { permissionArray: Array<String> ->
            Dexter.withActivity(ActivityUtils.getTopActivity())
                .withPermissions(*permissionArray)
                .withListener(
                    CompositeMultiplePermissionsListener(object :
                        BaseMultiplePermissionsListener() {
                        override fun onPermissionsChecked(report: MultiplePermissionsReport) {
                            super.onPermissionsChecked(report)
                            if (report.areAllPermissionsGranted()) {
                                vmBox.setAllPermissionsGranted()
                            } else if (report.isAnyPermissionPermanentlyDenied) {
                                SlcPopup2.createAlertDialogOperate().show(
                                    AlertDialog.Builder(context)
                                        .setMessage(R.string.label_permission_base_setting_hint)
                                        .setCancelable(false)
                                        .setPositiveButton(
                                            R.string.action_i_know,
                                            DialogInterface.OnClickListener { dialog, which ->
                                                vmBox.resetNormalRequestPermissionStatus()
                                                ActivityUtils.startActivity(
                                                    IntentUtils.getLaunchAppDetailsSettingsIntent(
                                                        AppUtils.getAppPackageName()
                                                    )
                                                )
                                            })
                                        .create()
                                )
                            } else {
                                SlcPopup2.createAlertDialogOperate().show(
                                    AlertDialog.Builder(context)
                                        .setMessage(R.string.label_permission_base_hint)
                                        .setCancelable(false)
                                        .setPositiveButton(
                                            R.string.action_i_know,
                                            DialogInterface.OnClickListener { dialog, which ->
                                                vmBox.resetNormalRequestPermissionStatus()
                                                vmBox.checkPermission()
                                            })
                                        .create()
                                )
                            }
                        }
                    })
                ).check()
        }
    }

    /**
     * 再次检查权限
     */
    fun againCheckPermission(){
        vmBox.checkPermission()
    }
}